﻿using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using LibKetoan;

namespace Ketoan
{
	public class frmRight_BV : System.Windows.Forms.Form
	{
		private DataSet ads;
		//private LibMedi.AccessData m = new LibMedi.AccessData();
		private LibKetoan.AccessData kt = new LibKetoan.AccessData();
		private string user = "";

		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Panel panel3;
		private System.Windows.Forms.Panel panel4;
		private System.Windows.Forms.Panel panel5;
		private System.Windows.Forms.TreeView treeView1;
		private System.Windows.Forms.Button butLuu;
		private System.Windows.Forms.Button butAll;
		private System.Windows.Forms.Button butHuy;
		private System.Windows.Forms.Button butKetthuc;
		private System.ComponentModel.Container components = null;

		public frmRight_BV(LibKetoan.AccessData m_m,TreeNode tn)
		{
			InitializeComponent();
			kt=m_m;
			treeView1.Nodes.Add(tn);

		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmRight_BV));
			this.panel1 = new System.Windows.Forms.Panel();
			this.panel2 = new System.Windows.Forms.Panel();
			this.panel3 = new System.Windows.Forms.Panel();
			this.panel4 = new System.Windows.Forms.Panel();
			this.panel5 = new System.Windows.Forms.Panel();
			this.butLuu = new System.Windows.Forms.Button();
			this.butAll = new System.Windows.Forms.Button();
			this.butHuy = new System.Windows.Forms.Button();
			this.butKetthuc = new System.Windows.Forms.Button();
			this.treeView1 = new System.Windows.Forms.TreeView();
			this.panel5.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
			this.panel1.Location = new System.Drawing.Point(0, 2);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(2, 566);
			this.panel1.TabIndex = 0;
			// 
			// panel2
			// 
			this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(622, 2);
			this.panel2.TabIndex = 0;
			// 
			// panel3
			// 
			this.panel3.Dock = System.Windows.Forms.DockStyle.Right;
			this.panel3.Location = new System.Drawing.Point(622, 0);
			this.panel3.Name = "panel3";
			this.panel3.Size = new System.Drawing.Size(2, 568);
			this.panel3.TabIndex = 0;
			// 
			// panel4
			// 
			this.panel4.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel4.Location = new System.Drawing.Point(0, 568);
			this.panel4.Name = "panel4";
			this.panel4.Size = new System.Drawing.Size(624, 2);
			this.panel4.TabIndex = 0;
			// 
			// panel5
			// 
			this.panel5.Controls.AddRange(new System.Windows.Forms.Control[] {
																				 this.butLuu,
																				 this.butAll,
																				 this.butHuy,
																				 this.butKetthuc});
			this.panel5.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel5.Location = new System.Drawing.Point(2, 536);
			this.panel5.Name = "panel5";
			this.panel5.Size = new System.Drawing.Size(620, 32);
			this.panel5.TabIndex = 1;
			// 
			// butLuu
			// 
			this.butLuu.Image = ((System.Drawing.Bitmap)(resources.GetObject("butLuu.Image")));
			this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butLuu.Location = new System.Drawing.Point(178, 5);
			this.butLuu.Name = "butLuu";
			this.butLuu.Size = new System.Drawing.Size(60, 25);
			this.butLuu.TabIndex = 0;
			this.butLuu.Text = "     &Lưu";
			this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
			// 
			// butAll
			// 
			this.butAll.Image = ((System.Drawing.Bitmap)(resources.GetObject("butAll.Image")));
			this.butAll.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butAll.Location = new System.Drawing.Point(241, 5);
			this.butAll.Name = "butAll";
			this.butAll.Size = new System.Drawing.Size(68, 25);
			this.butAll.TabIndex = 0;
			this.butAll.Text = "Chọn cả";
			this.butAll.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butAll.Click += new System.EventHandler(this.butAll_Click);
			// 
			// butHuy
			// 
			this.butHuy.Image = ((System.Drawing.Bitmap)(resources.GetObject("butHuy.Image")));
			this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butHuy.Location = new System.Drawing.Point(312, 5);
			this.butHuy.Name = "butHuy";
			this.butHuy.Size = new System.Drawing.Size(60, 25);
			this.butHuy.TabIndex = 0;
			this.butHuy.Text = "    &Hủy";
			this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
			// 
			// butKetthuc
			// 
			this.butKetthuc.Image = ((System.Drawing.Bitmap)(resources.GetObject("butKetthuc.Image")));
			this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butKetthuc.Location = new System.Drawing.Point(375, 5);
			this.butKetthuc.Name = "butKetthuc";
			this.butKetthuc.Size = new System.Drawing.Size(68, 25);
			this.butKetthuc.TabIndex = 0;
			this.butKetthuc.Text = "&Kết thúc";
			this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
			// 
			// treeView1
			// 
			this.treeView1.BackColor = System.Drawing.SystemColors.Info;
			this.treeView1.CheckBoxes = true;
			this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.treeView1.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.treeView1.ImageIndex = -1;
			this.treeView1.Location = new System.Drawing.Point(2, 2);
			this.treeView1.Name = "treeView1";
			this.treeView1.SelectedImageIndex = -1;
			this.treeView1.Size = new System.Drawing.Size(620, 534);
			this.treeView1.TabIndex = 2;
			this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
			// 
			// frmRight_BV
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(624, 570);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.treeView1,
																		  this.panel5,
																		  this.panel1,
																		  this.panel2,
																		  this.panel3,
																		  this.panel4});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "frmRight_BV";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Phân quyền bệnh viện sử dụng";
			this.Load += new System.EventHandler(this.frmRight_BV_Load);
			this.panel5.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmRight_BV_Load(object sender, System.EventArgs e)
		{
			user = kt.user;
			if (Screen.PrimaryScreen.WorkingArea.Width > 800) this.WindowState = FormWindowState.Normal;
			try
			{
				ads = kt.get_data("select * from " + user + ".a_menuitem order by id");
				int i = int.Parse(ads.Tables[0].Rows[0][0].ToString());
			}
			catch
			{
				kt.execute_data(" create table " + user + ".a_menuitem(id varchar2(5),id_goc varchar2(5),stt number(5) default 0,id_menu varchar2(5),ten nvarchar2(500)  ,constraint pk_m_menuitem primary key(id))");
				ads = kt.get_data("select * from " + user + ".a_menuitem order by id");
			}
			f_Set_Right();
		}
		private void f_Add_Idmenu_Cha(TreeNode v_node)
		{
			DataRow r;
			if (v_node.Parent != null)
			{
				if (ads.Tables[0].Select("id='" + v_node.Parent.Tag.ToString().PadLeft(3, '0') + "'").Length <= 0)
				{
					r = ads.Tables[0].NewRow();
					r["id"] = v_node.Parent.Tag.ToString().PadLeft(3, '0');
					r["id_menu"] = v_node.Parent.Tag.ToString().PadLeft(3, '0');
					r["ten"] = v_node.Parent.Text;
					r["stt"] = v_node.Parent.Index;
					if (v_node.Parent.Parent != null)
					{
						if (v_node.Parent.Parent.Tag.ToString() == "menuChucnang")
						{
							r["id_goc"] = "-1";
						}
						else r["id_goc"] = v_node.Parent.Parent.Tag.ToString().PadLeft(3, '0');
					}
					ads.Tables[0].Rows.Add(r);
				}
				if (v_node.Parent != null)
				{
					f_Add_Idmenu_Cha(v_node.Parent);
				}
			}
		}
		private void f_Remove_Idmenu_Cha(TreeNode v_node)
		{
			DataRow r;
			if (v_node.Parent != null)
			{
				foreach (TreeNode anode in v_node.Parent.Nodes)
				{
					if (!bCheck_Menu_Con(v_node.Parent))
					{
						r = kt.getrowbyid(ads.Tables[0], "id='" + v_node.Parent.Tag.ToString().PadLeft(3, '0') + "'");
						if (r != null) ads.Tables[0].Rows.Remove(r);
						break;
					}
					else break;

				}
				if (v_node.Parent != null)
				{
					f_Remove_Idmenu_Cha(v_node.Parent);
				}
			}

		}
		private bool bCheck_Menu_Con(TreeNode v_node)
		{
			foreach (TreeNode anode in v_node.Nodes)
			{
				if (anode.Checked)
				{
					return true;
				}
			}
			return false;
		}
		private void f_Set_Right()
		{
			foreach (TreeNode anode in treeView1.Nodes)
			{
				if (anode.Nodes.Count > 0) f_Set_Right(anode);
				else
				{
					if (ads.Tables[0].Select("id=" + anode.Tag.ToString()).Length > 0)
					{
						anode.Checked = true;
					}
				}
			}
		}
		private void f_Set_Right(TreeNode v_node)
		{

			try
			{
				foreach (TreeNode anode in v_node.Nodes)
				{
					if (anode.Nodes.Count > 0)
					{
						f_Set_Right(anode);
					}
					else
					{
						if (ads.Tables[0].Select("id=" + anode.Tag.ToString()).Length > 0)
						{
							anode.Checked = true;
						}
					}
				}
			}
			catch
			{
			}

		}

		private void f_Set_All(bool bAll)
		{
			foreach (TreeNode anode in treeView1.Nodes)
			{
				if (anode.Nodes.Count > 0)
				{
					f_Chon_Quyen(anode, bAll);
				}
			}
		}
		private void f_Chon_Quyen(TreeNode v_node, bool v_bool)
		{
			try
			{
				foreach (TreeNode anode in v_node.Nodes)
				{
					anode.Checked = v_bool;
					if (anode.Nodes.Count > 0)
					{
						f_Chon_Quyen(anode, v_bool);
					}
				}
			}
			catch
			{
			}
		}
	
		private void butAll_Click(object sender, EventArgs e)
		{
			butAll.Text = butAll.Text == "Chọn cả" ? "Bỏ cả" : "Chọn cả";
			if (butAll.Text == "Chọn cả") f_Set_All(false);
			else f_Set_All(true);
		}

		private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
		{

			DataRow r;
			if (ads.Tables[0].Select("id='" + e.Node.Tag.ToString().PadLeft(3, '0') + "'").Length <= 0)
			{
				if (e.Node.Checked)
				{
					r = ads.Tables[0].NewRow();
					r["id"] = e.Node.Tag.ToString().PadLeft(3, '0');
					r["id_menu"] = e.Node.Tag.ToString().PadLeft(3, '0');
					r["ten"] = e.Node.Text;
					r["stt"] = e.Node.Index;
					if (e.Node.Parent != null)
					{
						if (e.Node.Parent.Tag.ToString() == "menuChucnang")
						{
							r["id_goc"] = "-1";
						}
						else
						{
							r["id_goc"] = e.Node.Parent.Tag.ToString().PadLeft(3, '0');
						}
					}
					ads.Tables[0].Rows.Add(r);
				}
			}
			else
			{
				if (!e.Node.Checked)
				{
					r = kt.getrowbyid(ads.Tables[0], "id='" + e.Node.Tag.ToString().PadLeft(3, '0') + "'");
					if (r != null)
					{
						ads.Tables[0].Rows.Remove(r);
					}
				}

			}

			try
			{
				if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard)
				{
					f_Chon_Quyen(e.Node, e.Node.Checked);
					if (e.Node.Checked)
					{
						f_Add_Idmenu_Cha(e.Node);
					}
					else
					{
						f_Remove_Idmenu_Cha(e.Node);

					}
				}
			}
			catch
			{
			}

		}
		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
			 kt.execute_data("delete from " + user + ".a_menuitem");
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
			try
			{
				kt.execute_data("delete from " + user + ".a_menuitem");
				foreach (DataRow r in ads.Tables[0].Rows)
				{
					kt.upd_a_menuitem(r["id"].ToString(), r["id_menu"].ToString(), r["id_goc"].ToString(), decimal.Parse(r["stt"].ToString()), r["ten"].ToString());
				}
				foreach (DataRow r1 in kt.get_data("select * from a_dlogin").Tables[0].Rows)
				{
					string s = "";
					foreach (DataRow r2 in kt.get_data("select * from a_menuitem").Tables[0].Rows)
					{
						if (r1["right_"].ToString().IndexOf(r2["id_menu"].ToString() + "+") > -1)
						{
							s += r2["id_menu"].ToString() + "+";
						}
					}
					kt.execute_data("update a_dlogin set right_='" + s + "' where id=" + r1["id"].ToString() + "");
				}
				MessageBox.Show("Lưu thành công", kt.Msg, MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}
	}
}
